// pages/orderDetail/orderDetail.js
import {
  request
} from '../../request/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderDetail:{},
    second:'',
    minute:'',
    countdown:'', 
    endDate2:'',
    status:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let orderDetail = wx.getStorageSync('orderDetail');
    // let second = typeof wx.getStorageSync('time')==='string'? JSON.parse(wx.getStorageSync('time')):wx.getStorageSync('time');
    // let orderDetail = typeof wx.getStorageSync('orderDetail')==='string'? JSON.parse(wx.getStorageSync('orderDetail')):wx.getStorageSync('orderDetail');
    this.setData({
      orderDetail,
      endDate2:orderDetail.wash_end_time,
      status:orderDetail.order_state
    });
    var that = this;
    console.log(orderDetail.wash_end_time);
    that.countTime();
  },
  countTime() {
    var that = this;
    var date = new Date();
    var now = date.getTime();
    var endDate = new Date(that.data.endDate2);//设置截止时间
    var end = endDate.getTime();
    var leftTime = end - now; //时间差                        
    var m, s;
    if (leftTime >= 0) {
      // d = Math.floor(leftTime / 1000 / 60 / 60 / 24); 天
      // h = Math.floor(leftTime / 1000 / 60 / 60 % 24); //小时
      m = Math.floor(leftTime / 1000 / 60 % 60);
      s = Math.floor(leftTime / 1000 % 60);
      s = s < 10 ? "0" + s : s
      m = m < 10 ? "0" + m : m
      that.setData({
        countdown: m + "：" + s ,
      })
      // console.log(this.data.countdown);
     //递归每秒调用countTime方法，显示动态时间效果
     setTimeout(that.countTime, 1000);
    } else {
      request({
        url:'Order/getOrderById',
        data: {
          order_code: this.data.orderDetail.order_code
        },
      }).then((result) => {
        console.log(result);
        wx.setStorageSync('orderDetail', result.data.data);
        this.setData({
          orderDetail:result.data.data
        })
      });
    }
  },
  lookOrder(){
    wx.switchTab({
      url: '../../pages/active/active',
    })
  }
})